package com.ruoyi.business.shop.utils;


import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @ClassName: SMSSendUtils
 * @description: $
 * @author: GCC
 * @date: 2025/2/21 17:47
 * @Version: 1.0
 */

@Configuration
public  class SMSSendUtils {
    // 中国手机号码正则表达式
    private static final String CHINESE_PHONE_REGEX = "^1[3-9]\\d{9}$";
    private static final Pattern CHINESE_PHONE_PATTERN = Pattern.compile(CHINESE_PHONE_REGEX);

//    @Value("${ALIBABA_CLOUD_ACCESS_KEY_ID}"
    private   static  String ALIBABA_CLOUD_ACCESS_KEY_ID="LTAI5t8XQRFymxcrGkaFm94b";
//     @Value("${ALIBABA_CLOUD_ACCESS_KEY_SECRET}"
    private  static  String ALIBABA_CLOUD_ACCESS_KEY_SECRET ="UWHDverUQhtcbNZEgI9c9gnnfPBRpP";
    /**
     * 校验中国手机号码是否合法
     * @param phone 待校验的手机号码
     * @return 如果合法返回 true，否则返回 false
     */
    public static boolean isValidChinesePhone(String phone) {
        if (phone == null) {
            return false;
        }
        return CHINESE_PHONE_PATTERN.matcher(phone).matches();
    }


    public   static void sendMessage (String mobile,String code){


        if(!isValidChinesePhone(mobile)){
            throw new RuntimeException("手机号码不合规");
        }




      //关闭真实发送
      System.out.println("发送手机号："+mobile+"验证码:"+code);
      boolean a =true;
        if (a){
            return;
        }


        //去发送


        DefaultProfile profile = DefaultProfile.getProfile("cn-qingdao", ALIBABA_CLOUD_ACCESS_KEY_ID, ALIBABA_CLOUD_ACCESS_KEY_SECRET);


        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSignName("阿里云短信测试");
        request.setTemplateCode("SMS_154950909");
        request.setPhoneNumbers(mobile);
        // 使用 Gson 库创建正确的 JSON 字符串
        Gson gson = new Gson();
        Map<String, String> templateParam = new HashMap<>();
        templateParam.put("code", code);
        request.setTemplateParam(gson.toJson(templateParam));


//        String uuid = IdUtils.simpleUUID();

        //手机号作为key
//        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY +mobile;
//
//        redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);

//        Object cacheObject = redisCache.getCacheObject(verifyKey);

        try {
            SendSmsResponse sendSmsResponse = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(sendSmsResponse));

        } catch (ClientException e) {

            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

    };

}
